package game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class BaseMapa extends GameObject
{
	
	private static final long serialVersionUID = 1L;
	
	protected static Rectangle[] obstaculos;
	protected double velocity;
	public static Point sizeMap;
	public static Point2D mapPos;
	int posicaoInicialX, posicaoInicialY, posicaoFinalX, posicaoFinalY;
	
	
	
	
	
	public BaseMapa(Point sizeMap)
	{
		super(new Point(0,0), sizeMap);
		BaseMapa.sizeMap = sizeMap;
		super.setPosition(new Point2D.Double(0,0));
		this.velocity = Player.getSpeedConstante();
		this.setPosition(new Point2D.Double(0,0));
		posicaoInicialX = 380;
		posicaoInicialY = 280;
		mapPos = new Point2D.Double(0, 0);
		CarregarObstaculos(mapPos);
	}
	
	public static Rectangle[] getObstaculos() {
		return obstaculos;
	}
	
	public void Update()
	{
		
		Point2D vel = new Point2D.Double(0, 0);
	
		if(Player.pos.getX() < 200 && posicaoFinalX != 0)
		{
			vel = new Point2D.Double(velocity, 0);
		}
		if(Player.pos.getY() < 200 && posicaoFinalY != 0)
		{
			vel = new Point2D.Double(0, velocity);
		}
		if(Player.pos.getY() > 400 && posicaoFinalY > Game.screenHeigth - sizeMap.y)
		{
			vel = new Point2D.Double(0, -velocity);
		}
		if(Player.pos.getX() > 600 && posicaoFinalX > Game.screenWidth - sizeMap.x)
		{
			vel = new Point2D.Double(-velocity, 0);
		}
		if(posicaoFinalX == posicaoInicialX)
		{
			vel = new Point2D.Double(vel.getX(), 0);		
		}
		if(posicaoFinalY == posicaoInicialY)
		{
			vel = new Point2D.Double(0,vel.getY());		
		}
		
		posicaoFinalX += (int)vel.getX();
		posicaoFinalY += (int)vel.getY();
		
		super.addPosition(vel);
		MovimentarParalax(new Point2D.Double(vel.getX(), vel.getY()));
		GameObject.setVelMapa(vel);
	}
	
	public void MovimentarParalax(Point2D vel)
	{
		
		mapPos = new Point2D.Double(
				mapPos.getX() + vel.getX(),
				mapPos.getY() + vel.getY()
				);
			

		CarregarObstaculos(mapPos);
	}
	
	public void CarregarObstaculos(Point2D mapPos)
	{

		LinkedList<Rectangle> list = new LinkedList<Rectangle>();
		
		for (int x = 0; x < Map.GetInstance().Width(); x++) 
		{
			int x1 = x*40; 
					
			for (int y = 0; y < Map.GetInstance().Height(); y++) 
			{
				int y1 = y*40;
				
				if(Map.GetInstance().GetIndex(x, y) == 1)
				{
					
					list.add(new Rectangle(x1 + (int)mapPos.getX(),
							y1 + (int)mapPos.getY(),
							40,
							40));
							
				}
			}
			
		}
		
		int margemErro = 0;
		//lado esquerdo
		list.add(new Rectangle(-50 + (int)mapPos.getX() - margemErro,
				-100 + (int)mapPos.getY(),
				50, sizeMap.y + 200));
		
		//lado direito
		list.add(new Rectangle(sizeMap.x + (int)mapPos.getX() + margemErro,
				-100 + (int)mapPos.getY(),
				50, sizeMap.y + 200));
		
		// lado superior
		list.add(new Rectangle(-100 + (int)mapPos.getX(),
				-50 + (int)mapPos.getY() - margemErro,
				sizeMap.x + 200, 50));
		
		//lado inferior
		list.add(new Rectangle(-100 + (int)mapPos.getX(),
				sizeMap.y + (int)mapPos.getY() + margemErro,
				sizeMap.x + 200, 50));
		
		
		obstaculos = null;
		obstaculos = new Rectangle[list.size()];
		for (int i = 0; i < list.size(); i++) 
		{
			obstaculos[i] = list.get(i);
		}
	}

	public void draw(Graphics2D g2d)
	{
		super.draw(g2d);
		
		g2d.setColor(Color.RED);
		
		/*
		for (Rectangle inst : obstaculos) 
		{
			g2d.drawRect((int)inst.getMinX(), (int)inst.getMinY(), (int)inst.getWidth(), (int)inst.getHeight());
			
		}
		*/
		
	}
}